﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

using Pibby.Core.IO;
using Pibby.Core.Feeds;
using Pibby.Core.Persistence;

using dogd;

namespace Pibby.Core
{
	[Serializable]
	[Table("Subscription")]
	public class Podcast : Identifiable 
	{
		#region Constructors

		public Podcast()
		{
			this.Whuffie = new Whuffie();
			this.PublicationDate = DateTime.MinValue;
		}

		public Podcast(Uri url)
			: this (string.Empty, url)
		{
		}

		public Podcast(string name, Uri url)
		{
			this.Name = name;

			if (url != null)
			{
				this.Feed = url;

				if (string.IsNullOrEmpty (name))
					this.Name = this.Feed.AbsoluteUri;
			}

			this.Whuffie = new Whuffie();
			this.PublicationDate = DateTime.MinValue;
		}

		#endregion

		public override string ToString()
		{
			return string.Format (System.Globalization.CultureInfo.CurrentUICulture, "{0} : {1}", this.Name, this.Feed.AbsoluteUri);
		}

		#region Properties

		[PrimaryKeyColumn("SubscriptionID")]
		public int DbKey
		{
			get { return this.PrimaryKey.Value; }
			set { this.PrimaryKey.Value = value; }
		}

		public bool IsDbKeyValid
		{
			get { return this.PrimaryKey.IsValid; }
		}

		[Column("Name")]
		public string Name
		{
			get;
			set;
		}

		/// <summary>
		/// Use this when using name as part of a file name
		/// </summary>
		public string SafeName
		{
			get
			{
				return FileNameSanitizer.CamelCaseName (FileNameSanitizer.FixFileName(this.Name));
			}
		}

		
		public Uri Feed
		{
			get;
			set;
		}


		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings")]
		[Column("FeedUrl")]
		public string FeedUrl
		{
			get 
			{
				string url = string.Empty;

				if (this.Feed != null)
					url = this.Feed.AbsoluteUri;

				return url;
			}

			set
			{
				if (!string.IsNullOrEmpty (value))
					this.Feed = new Uri(value);
			}
		}

		
		//[Column("ImageUrl")]
		public Uri ImageUrl
		{
			get;
			set;
		}

		public string PictureLink
		{
			get 
			{
				if (this.ImageUrl == null)
					return string.Empty;

				return this.ImageUrl.AbsoluteUri; 
			}

			set
			{
				if (!string.IsNullOrEmpty(value))
				{
					this.ImageUrl = new Uri(value);
				}
			}
		}

		public Whuffie Whuffie
		{
			get;
			private set;
		}

		[IntegerColumn("Whuffie")]
		public int Score
		{
			get { return this.Whuffie.Score; }
			set { this.Whuffie.Score = value; }
		}

		[DateTimeColumn("LastUpdated")]
		public DateTime PublicationDate
		{
			get;
			set;
		}

		[Column ("Description")]
		public string Description
		{
			get;
			set;
		}

		#endregion

		#region Methods

		public bool RefreshInfo(Feed feed)
		{
			bool changed = false;

			if (feed != null)
			{
				if (this.ImageUrl == null)
				{
					this.ImageUrl = feed.ImageUrl;
					changed = true;
				}

				if (string.IsNullOrEmpty(this.Description))
				{
					this.Description = feed.Description;
					changed = true;
				}

				if (this.PublicationDate != feed.PublicationDate)
				{
					this.PublicationDate = feed.PublicationDate;
					changed = true;
				}
			}

			return changed;
		}

		#endregion

		#region Column Names

		public static string GuidColumn { get { return "Guid"; } }

		public static string NameColumn { get { return "Name"; } }
		
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings")]
		public static string FeedUrlColumn { get { return "FeedUrl"; } }

		public static string WhuffieColumn { get { return "Whuffie"; } }

		public static string LastUpdatedColumn { get { return "LastUpdated"; } }

		public static string DeletedColumn { get { return "Deleted"; } }

		public static string DescriptionColumn { get { return "Description"; } }

		#endregion Column Names
	}
}
